package application.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import application.models.Modwinetypes;

import library.Controller;

/**
 * Servlet implementation class catalog
 */
public class catalog extends Controller {
	private static final long serialVersionUID = 1L;
    
	@SuppressWarnings("unchecked")
	public void list(HttpServletRequest request, HttpServletResponse response, JspWriter out) throws ServletException, IOException{
		String name = this.getClass().getSimpleName();
		if(request.getSession().getAttribute("user")!= null) {
			//Prepare Session
			SessionFactory sessionFactory = new Configuration()
				.configure()
				.buildSessionFactory();
			//Start session
			Session session = sessionFactory.openSession();
			session.beginTransaction();
			

			List<Modwinetypes> catalog = (List<Modwinetypes>) session.createQuery("from Modwinetypes").list();
			request.setAttribute("catalog", catalog);
			
			out.flush();
			include(request,response,"/layout/"+name+"/list.jsp");
			
			session.getTransaction().commit();
			session.close();
		}else {
			response.sendRedirect("administrator?op=login");
		}	
	}
	
	@SuppressWarnings("unchecked")
	public void edit(HttpServletRequest request, HttpServletResponse response, JspWriter out) throws ServletException, IOException{
		String name = this.getClass().getSimpleName();
		
		if(request.getParameter("id") == null)
			response.sendRedirect(name+"?op=list");
		String type_id = (String) request.getParameter("id");
		
		if(request.getSession().getAttribute("user")!= null) {
			//Prepare Session
			SessionFactory sessionFactory = new Configuration()
				.configure()
				.buildSessionFactory();
			//Start session
			Session session = sessionFactory.openSession();
			session.beginTransaction();
			
			Query sql = session.createQuery("from Modwinetypes where type_id =:id");
			List<Modwinetypes> winerys = (List<Modwinetypes>) sql.setString("id", type_id).list();
			if(winerys.isEmpty())
				response.sendRedirect(name+"?op=list");
			else {
				out.flush();
				request.setAttribute("catalog", (Modwinetypes) winerys.get(0));
				include(request,response,"/layout/"+name+"/edit.jsp");
			}
			
			session.getTransaction().commit();
			session.close();			
		}else {
			response.sendRedirect("administrator?op=login");
		}
	}

	
	public void add(HttpServletRequest request, HttpServletResponse response, JspWriter out) throws ServletException, IOException{
		String name = this.getClass().getSimpleName();
		if(request.getSession().getAttribute("user")!= null) {
			out.flush();
			request.setAttribute("winery", null);
			include(request,response,"/layout/"+name+"/edit.jsp");
		}else {
			response.sendRedirect("administrator?op=login");
		}
	}
	
	@SuppressWarnings("unchecked")
	public void delete(HttpServletRequest request, HttpServletResponse response, JspWriter out) throws ServletException, IOException {
		String name = this.getClass().getSimpleName();
		String type_id = (String) request.getParameter("id");
		if(request.getSession().getAttribute("user")!= null) {
			//Prepare Session
			SessionFactory sessionFactory = new Configuration()
				.configure()
				.buildSessionFactory();
			//Start session
			Session session = sessionFactory.openSession();
			session.beginTransaction();
			
			Query sql = session.createQuery("from Modwinetypes where type_id =:id");
			List<Modwinetypes> winerys = (List<Modwinetypes>) sql.setString("id", type_id).list();
			session.delete(winerys.get(0));
			
			session.getTransaction().commit();
			session.close();
			
			response.sendRedirect(name+"?op=list");
		} else {
			response.sendRedirect("administrator?op=login");
		}
	}
	
	@SuppressWarnings({ "unchecked" })
	public void save(HttpServletRequest request, HttpServletResponse response, JspWriter out) throws ServletException, IOException{
		try {
			String name = this.getClass().getSimpleName();
			if(request.getSession().getAttribute("user")!= null) {
				//Prepare Session
				SessionFactory sessionFactory = new Configuration()
					.configure()
					.buildSessionFactory();
				//Start session
				Session session = sessionFactory.openSession();
				session.beginTransaction();			
				if(request.getParameter("id")!= null){
					String type_id = (String) request.getParameter("id");
					Query sql = session.createQuery("from Modwinetypes where type_id =:id");
					
					List<Modwinetypes> catalogs = (List<Modwinetypes>) sql.setString("id", type_id).list();
					
					if(catalogs.isEmpty()) {
						response.sendRedirect(name+"?op=list");
					} else {
						session.delete(catalogs.get(0));
					}
				}
				
				Modwinetypes catalog = new Modwinetypes();
				catalog.setTypeId(request.getParameter("catalog"));
				catalog.setDescription(request.getParameter("description"));
				
				Query sql = session.createQuery("from Modwinetypes where type_id =:id");
				List<Modwinetypes> maintype = (List<Modwinetypes>) sql.setString("id", request.getParameter("intoGroup")).list();
				
				if(!maintype.isEmpty())
					catalog.setModwinetypes(maintype.get(0));
				
				session.save(catalog);
				response.sendRedirect(name+"?op=list");
				
				session.getTransaction().commit();
				session.close();			
			}else {
				response.sendRedirect("administrator?op=login");
			}
		}catch(Exception e){
			PrintWriter x = response.getWriter();
			x.write(e.toString()+":"+e.getLocalizedMessage());
		}
	}

}
